#include <ctype.h>
#include <stdlib.h>

int getbit(void * bmp, int n)
{
    unsigned char x, * p;
    int i, j;
    p = (unsigned char *)bmp;

    i = n / 8;
    j = n % 8;

    x = (p[i] >> j) & 1;

    return x;
}

void setbit(void * bmp, int n)
{
    unsigned char x, * p;
    int i, j;
    p = (unsigned char *)bmp;

    i = n / 8;
    j = n % 8;

    x = (1 << j);
    p[i] |= x;

    return;
}

void clrbit(void * bmp, int n)
{
    unsigned char x, * p;
    int i, j;
    p = (unsigned char *)bmp;

    i = n / 8;
    j = n % 8;

    x = (1 << j);
    p[i] &= ~x;

    return;
}

link_node * nodenext(link_node * node)
{
    return (node->next);
}

link_node * nodeprev(link_node * node)
{
    return (node->prev);
}

link_node * nodehead(link_node * node)
{
    while(node->prev != NULL)
        {
        node = node->prev;
        }

    return node;
}

link_node * nodetail(link_node * node)
{
    while(node->next != NULL)
        {
        node = node->next;
        }

    return node;
}

link_node * nodeaddto(link_node * node, link_node * prev, link_node * next)
{
    if (node == NULL)
        return node;

    node->prev = prev;
    node->next = next;
    if (prev != NULL)
        {
        prev->next = node;
        }

    if (next != NULL)
        {
        next->prev = node;
        }

    return node;
}

link_node * nodedelwith(link_node * node, link_node * prev, link_node * next)
{
    if (node == NULL)
        return node;

    if (prev != NULL)
        {
        prev->next = next;
        }

    if (next != NULL)
        {
        next->prev = prev;
        }

    node->prev = NULL;
    node->next = NULL;

    return node;
}

link_node * nodeadd(link_node * node, link_node * prev)
{
    return nodeaddto(node, prev, prev->next);
}

link_node * nodeaddtotail(link_node * node, link_node * prev)
{
    return nodeaddto(node, nodetail(prev), NULL);
}

link_node * nodeaddtohead(link_node * node, link_node * prev)
{
    return nodeaddto(node, NULL, nodehead(prev));
}

link_node * nodedel(link_node * node)
{
    return nodedelwith(node, node->prev, node->next);
}

link_node * nodedelhead(link_node * node)
{
    node = nodehead(node);
    return nodedel(node);
}

link_node * nodedeltail(link_node * node)
{
    node = nodetail(node);
    return nodedel(node);
}

link_node * nodeinit(link_node * node)
{
    node->prev = NULL;
    node->next = NULL;

    return node;
}

